package com.longxi.tianjintianbao.ui.base;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.widget.EmptyLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

public abstract class LazyFragment<V, T extends BasePresenter<V>> extends Fragment implements IBaseView, EmptyLayout.OnRetryListener {
    /**
     * 把 EmptyLayout 放在基类统一处理，@Nullable 表明 View 可以为 null，详细可看 ButterKnife
     */
    @Nullable
    @BindView(R.id.tvLeft)
    protected TextView mTvLeft;
    @Nullable
    @BindView(R.id.ivLeft)
    protected ImageView mIvLeft;
    @Nullable
    @BindView(R.id.tvBarTitle)
    protected TextView mTvBarTitle;
    @Nullable
    @BindView(R.id.tvRight)
    protected TextView mTvRight;
    @Nullable
    @BindView(R.id.ivRight)
    protected ImageView mIvRight;
    @Nullable
    @BindView(R.id.ivRight2)
    protected ImageView mIvRight2;

    @Nullable
    @BindView(R.id.empty_layout)
    protected EmptyLayout mEmptyLayout;
    @Nullable
    @BindView(R.id.refreshLayout)
    protected SmartRefreshLayout mRefreshLayout;
    @Nullable
    @BindView(R.id.rvContent)
    protected RecyclerView mRvContent;

    private View view;

    private boolean needInit;      //是否需要在onCreateView中初始化组件
    private boolean hasLoad;      //标识是否已经加载过
    private boolean hasCreated;  //是否为第一次加载数据

    protected T mPresenter;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        //展示 & 没有加载过
        if (isVisibleToUser && !hasLoad) {
            //如果当前Fragment向用户展示且没有加载过，进行下一步判断
            if (hasCreated) {
                //如果onCreateView已经被创建，此时进行加载
                initView();
            } else {
                //如果Fragment没有创建，那么设置标记，在onCreateView中加载
                needInit = true;
            }
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        //判断是否使用MVP模式
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachView((V) this);//因为之后所有的子类都要实现对应的View接口
        }
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(provideContentViewId(), container, false);
        if (needInit) {
            //是否要初始化，后面页不需要
            initView();
            needInit = false;
        }
        hasCreated = true;
        return view;
    }

    private void initView() {
        ButterKnife.bind(this, view);
        init(view);
        initListener();
        hasLoad = true;
    }

    public void initListener() {

    }

    //用于创建Presenter和判断是否使用MVP模式(由子类实现)
    protected abstract T createPresenter();

    //获取Fragment的布局文件
    @LayoutRes
    protected abstract int provideContentViewId();

    //包含初始化数据
    protected abstract void init(View view);

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter = null;
        }
    }

    @Override
    public void showLoading() {
        if (mEmptyLayout != null) {
            mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_LOADING);
        }
    }

    @Override
    public void hideLoading() {
        if (mEmptyLayout != null) {
            mEmptyLayout.hide();
        }
    }

    @Override
    public void showNetError() {
        if (mEmptyLayout != null) {
            mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_NO_NET);
            mEmptyLayout.setRetryListener(this);
        }
    }

    @Override
    public void showNoData() {
        if (mEmptyLayout != null) {
            mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_NO_DATA);
        }
    }

    @Override
    public void showError() {
        if (mEmptyLayout != null) {
            mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_ERROR);
        }
    }

    @Override
    public void onRetry() {

    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void finishLoadMore() {

    }

    @Override
    public void setTitle(String title) {
        if(mTvBarTitle != null){
            mTvBarTitle.setVisibility(View.VISIBLE);
            mTvBarTitle.setText(title);
        }
    }

    @Override
    public void setTvLeft(String name) {

    }

    @Override
    public void setIvLeft(int image) {

    }

    @Override
    public void setTvRight(String name) {

    }

    @Override
    public void setIvRight(int image) {
        if(mIvRight != null){
            mIvRight.setVisibility(View.VISIBLE);
            mIvRight.setImageResource(image);
        }
    }

    @Override
    public void setIvRight2(int image) {
        if(mIvRight2 != null){
            mIvRight2.setVisibility(View.VISIBLE);
            mIvRight2.setImageResource(image);
        }
    }
}
